var setting={
    autoLogin:0,
    setVpn:function(ip,passport,password){
        var param={
            ip:ip,
            passport:passport,
            password:password
        };
        vpn.openVpn(function(su){
                $.AMUI.progress.done();
		},
		function(er){
            $.AMUI.progress.done();
		    $('#loginTip p:first').text("登录异常，请重新登录");
		    $("#loginTip").show();
			$('#loginTip').delay(6000).hide(0);
		},param);
    },
    closeVpn:function(){
        vpn.closeVpn(function(su){alert("成功关闭");},function(er){
            alert("关闭失败");
        });
    },
    changePwd:function(oldPwd,newPwd){
        var param={
            newPwd:newPwd,
            oldPwd:oldPwd
        };
        vpn.changePwd(function(su){alert("密码修改成功");},function(er){
            alert("密码修改失败");
        },param);
    },

    // Application Constructor
    initialize: function (autoLogin) {
        if(autoLogin)
            this.autoLogin=autoLogin;
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function () {
        setting.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function (id) {
        var vpnCookie = localStorage.VPN_REMEMBER_ME_COOKIE_;
        if(vpnCookie){
            vpnCookie=JSON.parse(vpnCookie);
            $('#ip').val(vpnCookie.ip);
            $('#name').val(vpnCookie.name);
            $('#pass').val(vpnCookie.pass);

            if(this.autoLogin==0&&false) {
                $.AMUI.progress.start();
                setting.setVpn(vpnCookie.ip, vpnCookie.name, vpnCookie.pass);
            }
        }
    },
    getQueryStringByName:function(name){
        var result = location.search.match(new RegExp("[\?\&]" + name+ "=([^\&]+)","i"));
        if(result == null || result.length < 1){
            return "";
        }
        return result[1];
    }
};
